<%@page import="sosr.serveur.IServeurRMI"%>
<%@page import="java.io.File"%>
<%@page import="sosr.serveur.ServeurRMI"%>
<%@page import="sosr.pdf.CreatePDF"%>
<%@page import="sosr.bean.Medecin"%>
<%@page import="sosr.bean.Medoc"%>
<%@page import="java.util.ArrayList"%>
<%@page import="sosr.bean.Ordonnance"%>
<%@page import="sosr.bean.OrdonnanceBDD"%>
<%@page import="sosr.bean.Patient"%>
<%@page import="bean.Valider"%>
<jsp:useBean id="pageConfig" scope="session" class="bean.Page"></jsp:useBean>
<jsp:useBean id="pageCGI" scope="session" class="bean.Page"></jsp:useBean>
<jsp:useBean id="logon" scope="session" class="bean.Connexion"></jsp:useBean>
<% 
int id = pageConfig.getAction_id(); 
%>

<%
int idp = -1;

String nom_medoc = "";
String poso_medoc = "";

IServeurRMI serv = logon.getSrvRMI();
serv.open();

request.setCharacterEncoding("UTF-8");
if (request.getParameter("submit") != null) {
	//boolean res = Valider.form(request, patient);
	boolean res = true;
	//get patient
	idp = Integer.parseInt(request.getParameter("idpatient"));
		Patient patient = serv.selectPatient(idp);
		serv.close();
	//get medecin
	Medecin medecin = pageConfig.getMedecin();
	//get medocs
	ArrayList<Medoc> medication = new ArrayList<Medoc>();
	String strmedoc = request.getParameter("medoc");
	String[] lstmedoc = strmedoc.split("###");
	for (int j=1; j < lstmedoc.length; j++) {
		String[] medoc = lstmedoc[j].split("##");
		Medoc med = new Medoc(Integer.parseInt(medoc[0]),medoc[1],medoc[2]);
		medication.add(med);
	}
	if (res) {
		System.out.println("id medecin ::  "+medecin.getId());
		Ordonnance ordonnance = new Ordonnance(0,medecin,patient,medication);
		serv.open();
		boolean rp = serv.addOrdonnance(ordonnance);
		ordonnance.setIdOrdo(serv.maxOrdonnance());
		String path = request.getRealPath("/")+"patients/"+patient.getIdPatient()+"/ordonnances/";
		File f = new File(path);
		if (!f.exists()) { f.mkdirs(); }
		CreatePDF pdf = new CreatePDF(path,ordonnance);
		pdf.generate();
		serv.close();
		if (rp) {
			pageCGI.setMessage("Notification","L'ordonnance du patient "+patient.getCivilite()+" "+patient.getPrenom()+" "+patient.getNom()+" &agrave; &eacute;t&eacute; cr&eacute;&eacute;e avec succ&egrave;s !");
			response.sendRedirect("../../ordonnance.jsp");
		}
	}
}
else {

%>

<h1>Cr&eacute;ation d'une ordonnance</h1>

<form action="pages/ordonnance/form_newordonnance.jsp" method="post" id="formordo">
<input type="hidden" name="id" value="<%=id %>" />
<table cellpadding="0" cellspacing="0" class="main_form">
	<tr>
		<td>Patient *</td>
		<td>
			<input type="hidden" name="idpatient" value="" id="idpatient" class="field" />
			<input type="text" name="patient_nom" id="patient_nom" value="" class="field" />
		</td>
	</tr>
	<tr>
		<td>M&eacute;dicament(s) *&nbsp;</td>
		<td></td>
	</tr>
	<tr>
		<td colspan="2" style="text-align: right">
			<div class="lst_medoc"></div>
			<input type="hidden" name="medoc_id" value="" id="medoc_id" class="field" />
			<div style="margin-bottom: 5px">Nom <input type="text" name="medoc_nom" id="medoc_nom" value="" class="field ac_input" /></div>
			<div style="margin-bottom: 5px">Posologie <input type="text" name="medoc_poso" id="medoc_poso" value="" class="field" /></div>
			<div><input type="button" value="Ajouter le m&eacute;dicament" class="bouton" onclick="addMedic()" /></div>
		</td>
	</tr>
	<tr><td>&nbsp;</td><td></td></tr>
	<tr><td colspan="2"><em>* champs obligatoires</em></td></tr>
	<tr><td>&nbsp;</td><td></td></tr>
	<tr>
		<td colspan="2">
			<input type="submit" class="bouton" name="submit" value="Enregistrer" style="float: right" onclick="fillMedoc();" />
			<input type="reset" class="bouton" value="R&eacute;initialiser" style="float: right" />
		</td>
	</tr>
</table>
<input type="hidden" name="medoc" value="" id="medoc_h" />
</form>

<% } %>
<script type="text/javascript">
$("#formordo").submit(function() {
    if (check()) {
      return true;
    }
    alert2("Veuillez renseigner l'ensemble des champs");
    return false;
  });

function check() {
	if ($("#identifiant").val() == ""
		|| $("#medoc_h").val() == "") {
		return false;
	}
	else {
		return true;
	}
}
</script>
<script type="text/javascript">
var i = 0;

var medics = [];

function addMedic () {
	var nom = $("#medoc_nom").val();
	var poso = $("#medoc_poso").val();
	var id = $("#medoc_id").val();
	
	if (nom == "" || poso == "") return;
	
	$(".lst_medoc").append("<div class='bloc_medic' id='medic_"+i+"'>"+"<div class='del_medic' onclick='dropMedic("+i+")'></div>"+"<div class='nom_medic'>"+nom+"</div>"+"<div class='poso_medic'>"+poso+"</div>"+"</div>");
	
	//var medoc_h = $("#medoc_h").val();
	//$("#medoc_h").val(medoc_h+"##"+id+"||"+nom+"||"+poso);
	medics[i] = "###"+id+"##"+nom+"##"+poso;
	
	
	$("#medoc_nom").val("");
	$("#medoc_poso").val("");
	i++;
}
function dropMedic (id) {
	$("#medic_"+id).remove();
	medics[id] = "";
}

function fillMedoc() {
	for (j=0; j<i; j++) {
		var md = medics[j];
		if (md != "") {
			var medoc_h = $("#medoc_h").val();
			$("#medoc_h").val(medoc_h+md);
		}
	}
	$("#formordo").submit();
}

$(document).ready(function() {
	$("#medoc_nom").attr("autocomplete", "on");
});

function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("The value you selected was: " + sValue);
}

function selectItem(li) {
  	findValue(li);
  	$("#medoc_id").val(li.extra[0]);
  	$("#medoc_poso").val(li.extra[1]);
}

function selectItemp(li) {
  	findValue(li);
  	$("#idpatient").val(li.extra[0]);
}

function formatItem(row) {
  	return row[0];
}

function lookupAjax(){
	var oSuggest = $("#medoc_nom")[0].autocompleter;
  oSuggest.findValue();
	return false;
}

function lookupLocal(){
  	var oSuggest = $("#medoc_nom")[0].autocompleter;

  	oSuggest.findValue();

  	return false;
}


  $("#medoc_nom").autocomplete(
    "autocomplete/autocomplete_medoc.jsp",
    {
			delay:10,
			minChars:1,
			matchSubset:1,
			matchContains:1,
			cacheLength:10,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:true
		}
  );
  
  $("#patient_nom").autocomplete(
    "autocomplete/autocomplete_patient.jsp",
    {
			delay:10,
			minChars:1,
			matchSubset:1,
			matchContains:1,
			cacheLength:10,
			onItemSelect:selectItemp,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:true
		}
  );

  
</script>